﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;


/// <summary>
/// 返回結果
/// </summary>
[DataContract]
class RouteStop_Result {
    /// <summary>
    /// 站牌ID
    /// </summary>
    [DataMember(Order = 0)]
    public int id { get; set; }
    /// <summary>
    /// 站牌名稱
    /// </summary>
    [DataMember(Order = 1)]
    public string name { get; set; }
    /// <summary>
    /// Map坐標
    /// </summary>
    [DataMember(Order = 2)]
    public string lat { get; set; }
    /// <summary>
    ///  Map坐標
    /// </summary>
    [DataMember(Order = 3)]
    public string lng { get; set; }
    /// <summary>
    /// 站牌位置
    /// </summary>
    [DataMember(Order = 4)]
    public int seq { get; set; }
    /// <summary>
    /// 去回程
    /// </summary>
    [DataMember(Order = 5)]
    public int gob { get; set; }
    /// <summary>
    /// 到站时间
    /// </summary>
    [DataMember(Order =6)]
    public int estime { get; set; }
    [DataMember(Order = 7)]
    public IList<RouteStop_Bus> bus { get; set; }
}
[DataContract]
class RouteStop_Bus
{
    [DataMember(Order = 0)]
    public string id { get; set; }
    [DataMember(Order = 1)]
    public string crowded { get; set; }
}

[DataContract]
class RouteStop_Stop
{
    [DataMember(Order = 0)]
    public IList<RouteStop_Result> stop { get; set; }
}

public partial class Service_RouteStop : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request.QueryString["rtid"]) && !string.IsNullOrEmpty(Request.QueryString["city"]))
        {
            using (MyDb.Db db = new MyDb.Db())
            {
                //RouteStop_Stop stop = new RouteStop_Stop();

                //stop.stop = new List<RouteStop_Result>();
                List<RouteStop_Result> result = new List<RouteStop_Result>();
                RouteStop_Result item = null;
                string sql = "";

                if (!string.IsNullOrEmpty(Request.QueryString["gob"]))
                    sql = string.Format("select b.Id,b.nameZh,b.latitude,b.longitude,b.city,b.goBack,(select top 1 EstimateTime from EstimateTime where StopID=b.Id and GoBack=b.goBack order by datatime desc) as estime,a.sequenceNo from Stop b left join PathDetail a on a.stopId = b.Id where a.pathAttributeId='{0}' and b.city='{1}' and b.goBack='{2}' order by b.goBack asc,a.sequenceNo asc", Request.QueryString["rtid"], Request.QueryString["city"], Request.QueryString["gob"]);
                else
                    sql = string.Format("select b.Id,b.nameZh,b.latitude,b.longitude,b.city,b.goBack,(select top 1 EstimateTime from EstimateTime where StopID=b.Id and GoBack=b.goBack order by datatime desc) as estime,a.sequenceNo from Stop b left join PathDetail a on a.stopId = b.Id where a.pathAttributeId='{0}' and b.city='{1}' order by b.goBack asc,a.sequenceNo asc", Request.QueryString["rtid"], Request.QueryString["city"]);

                using (DataTable dt = db.getDataTable(sql, "mess"))
                {
                    foreach (DataRow rs in dt.Rows)
                    {
                        item = new RouteStop_Result();
                        item.name = rs["nameZh"].ToString();
                        item.id = Convert.ToInt32(rs["Id"]);
                        item.lat = rs["latitude"].ToString();
                        item.lng = rs["longitude"].ToString();
                        item.seq = Convert.ToInt32(rs["sequenceNo"]);
                        item.gob = Convert.ToInt32(rs["goBack"]);
                        item.estime = string.IsNullOrEmpty(rs["estime"].ToString())?0:Convert.ToInt32(rs["estime"]);
                        item.bus = new List<RouteStop_Bus>();
                        using (DataTable dt2 = db.getDataTable(string.Format("select a.BusID,b.Crowded from Busevent a left join bus b on b.BusID=a.BusID where a.RouteID='{0}' and a.StopID='{1}' and a.City='{2}' and a.GoBack='{3}'",
                            Request.QueryString["rtid"],rs["Id"], Request.QueryString["city"], Request.QueryString["gob"]), "mess"))
                        {
                            foreach (DataRow rs2 in dt2.Rows)
                            {
                                RouteStop_Bus v = new RouteStop_Bus();
                                v.id = rs2["BusID"].ToString();
                                v.crowded = rs2["Crowded"].ToString();
                                item.bus.Add(v);
                            }
                        }
                        result.Add(item);
                    }
                }
                Response.Write(MyDb.JSON.stringify(result));
                result.Clear();
                result = null;
            }
        }
        Response.End();
    }
}
